#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.security.oauth2.server;

import ${package}.basic.security.oauth2.config.YkAuthExceptionEntryPoint;
import ${package}.basic.security.oauth2.config.YkSecurityProperties;
import ${package}.basic.security.oauth2.handler.YkAccessDeniedHandler;
import ${package}.basic.security.oauth2.inter.AuthorizeConfigManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * Description:
 *     资源服务器的配置类
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Configuration
@EnableResourceServer
public class YkAuthOauth2ResourceServerAuthConfig extends ResourceServerConfigurerAdapter {

    /**
     * TokenStore
     */
    private TokenStore tokenStore;
    /**
     * YkAuthExceptionEntryPoint
     */
    private YkAuthExceptionEntryPoint ykAuthExceptionEntryPoint;
    /**
     * 权限句柄管理器
     */
    private AuthorizeConfigManager authorizeConfigManager;
    /**
     * spring EL 表达式处理器
     */
    private OAuth2WebSecurityExpressionHandler expressionHandler;
    /**
     * 没有权限的访问处理器
     */
    private YkAccessDeniedHandler ykAccessDeniedHandler;
    /**
     * 安全配置
     */
    private YkSecurityProperties ykSecurityProperties;


    /**
     * Description:
     * <配置哪些接口可以不用认证>
     * @author ywh
     * @date 13:48 2019/8/7
     * @param httpSecurity 1
     **/
    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {

        httpSecurity.authorizeRequests()
                .antMatchers(ykSecurityProperties.getAllowPath()).permitAll()
                .antMatchers(ykSecurityProperties.getLoginProcessUrl(), ykSecurityProperties.getOssLoginPage()).permitAll()
                .antMatchers("/yk_api/**").access("@ykUrlPermission.hasUrlPermission(request, authentication)")
                .and()
            .cors()
                .disable()
            .csrf()
                .disable()
        ;
    }

    /**
     * Description:
     * <资源服务器配置>
     * @author ywh
     * @date 13:49 2019/8/7
     * @param resources 1
     **/
    @Override
    public void configure(ResourceServerSecurityConfigurer resources){
        resources.resourceId(ykSecurityProperties.getResourceId())
            .tokenStore(tokenStore)
            // token失效或者缺失异常
            .authenticationEntryPoint(ykAuthExceptionEntryPoint)
            // 表达式解析器
            .expressionHandler(expressionHandler)
            // token权限不足的异常
            .accessDeniedHandler(ykAccessDeniedHandler);
    }

    @Autowired
    public void setTokenStore(TokenStore tokenStore) {
        this.tokenStore = tokenStore;
    }

    @Autowired
    public void setYkAuthExceptionEntryPoint(YkAuthExceptionEntryPoint ykAuthExceptionEntryPoint) {
        this.ykAuthExceptionEntryPoint = ykAuthExceptionEntryPoint;
    }

    @Autowired
    public void setAuthorizeConfigManager(AuthorizeConfigManager authorizeConfigManager) {
        this.authorizeConfigManager = authorizeConfigManager;
    }

    @Autowired
    public void setExpressionHandler(OAuth2WebSecurityExpressionHandler expressionHandler) {
        this.expressionHandler = expressionHandler;
    }

    @Autowired
    public void setYkAccessDeniedHandler(YkAccessDeniedHandler ykAccessDeniedHandler) {
        this.ykAccessDeniedHandler = ykAccessDeniedHandler;
    }

    @Autowired
    public void setYkSecurityProperties(YkSecurityProperties ykSecurityProperties) {
        this.ykSecurityProperties = ykSecurityProperties;
    }

}
